//
//  SDMainPageVC.m
//  SDHuiYuShoping
//
//  Created by luojiao on 2017/4/12.
//  Copyright © 2017年 luojiao. All rights reserved.
//

#import "SDMainPageVC.h"
#import "MainPageTableViewCell.h"
#import "JLAdvertisingScrollView.h"//thrid scrollView
#import "HotproductViewController.h"
#import "NewProductViewController.h"
#import "GoodProductVC.h"
#import "ViewController.h"

#import "ProductViewController.h"


@interface SDMainPageVC ()<UITableViewDelegate,UITableViewDataSource,JLAdvertisingScrollViewDelegate,ChoseProductDelegate>

{
    NSMutableArray *sectionImageArr;
    NSMutableArray *cellImageArr;
    NSMutableArray *cellNameArr;
    NSMutableArray *cellPriceArr;
    NSMutableArray *bannerImageArr;
}

@end

@implementation SDMainPageVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.hidden = YES;
    self.tableView.tableHeaderView = self.headerView;
    self.tableView.separatorStyle = NO;
    self.searchTextField.delegate = self;
    bannerImageArr = [[NSMutableArray alloc] initWithObjects:@"text_twelve",@"text_twelve",@"text_twelve" ,nil];
    
    //滚动的 scrollView
    JLAdvertisingScrollView *scrollView = [[JLAdvertisingScrollView alloc] initWithFrame:CGRectMake(0, 0, APP_WIDTH, self.headerView1.size.height)];
    [scrollView setImageNames:bannerImageArr];
    [self.headerView1 addSubview:scrollView];
    [scrollView setDelegate:self];
    [scrollView setAutoLoopInterval:3];

    
    sectionImageArr = [[NSMutableArray alloc] initWithObjects:@"new_msg",@"love_msg",@"enjoy_msg", nil];
    cellImageArr = [[NSMutableArray alloc] initWithObjects:@"text_seven",@"text_six",@"text_ten", nil];
    cellNameArr = [[NSMutableArray alloc] initWithObjects:@"萤石硬盘录像",@"vivo X9 4G手机",@"男士自动扣腰带" ,nil];
    cellPriceArr = [[NSMutableArray alloc] initWithObjects:@"9000",@"3999",@"229" ,nil];

    
    //设置按钮
    [self setButtonImage];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    //设置状态栏颜色黑色
    self.tabBarController.tabBar.hidden = NO;
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    //设置tabbar底部的商品数量
    [[[[[self tabBarController] viewControllers] objectAtIndex:1] tabBarItem] setBadgeValue:@"2"];
}

#pragma mark - ButtonClick

- (IBAction)searchButtonClick:(id)sender
{
    
}

- (IBAction)newproductButton:(id)sender
{
 
    NewProductViewController *newProductVC = [[NewProductViewController alloc] init];
    [self.navigationController pushViewController:newProductVC animated:YES];
}

- (IBAction)hotProductButton:(id)sender
{
    HotproductViewController *hotProductVC = [[HotproductViewController alloc] init];
    [self.navigationController pushViewController:hotProductVC animated:YES];
    
}


- (IBAction)goodProductButton:(id)sender
{
    GoodProductVC *goodProductVC = [[GoodProductVC alloc] init];
    [self.navigationController pushViewController:goodProductVC animated:YES];
}


- (void)setButtonImage
{
    [self.newsButton setImage:[UIImage imageNamed:@"new_btn"] forState:UIControlStateNormal];
    [self.newsButton setImageEdgeInsets:UIEdgeInsetsMake(-30, 0, 0, 0)];
    [self.newsButton setTitle:@"新品推荐" forState:UIControlStateNormal];
    [self.newsButton setTitleEdgeInsets:UIEdgeInsetsMake(0, -80, -80, 0)];

    
    [self.hotButton setImage:[UIImage imageNamed:@"hot_btn"] forState:UIControlStateNormal];
    [self.hotButton setImageEdgeInsets:UIEdgeInsetsMake(-30, 0, 0, 0)];
    [self.hotButton setTitle:@"热销商品" forState:UIControlStateNormal];
    [self.hotButton setTitleEdgeInsets:UIEdgeInsetsMake(0, -80, -80, 0)];

    [self.goodButton setImage:[UIImage imageNamed:@"good_btn"] forState:UIControlStateNormal];
    [self.goodButton setImageEdgeInsets:UIEdgeInsetsMake(-30, 0, 0, 0)];
    [self.goodButton setTitle:@"优选品牌" forState:UIControlStateNormal];
    [self.goodButton setTitleEdgeInsets:UIEdgeInsetsMake(0, -80, -80, 0)];
    
    [self.searchImage setImage:[UIImage imageNamed:@"found_btn"]];
    [self.searchBGImage setImage:[FRUtils resizeImageWithImageName:@"search_bg"]];
    [self.searchButton setTitle:@"搜 索" forState:UIControlStateNormal];
    [self.searchButton setTitleEdgeInsets:UIEdgeInsetsMake(0, -20, 0, 0)];
    
    [self.searchButton.titleLabel setFont:[UIFont systemFontOfSize:15]];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UITextFieldDelegate

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}


#pragma mark - UITableViewDelegate,UITableViewDataSource

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1)
    {
        if (indexPath.row != 2)
        {
            return 115;
        }
        return 150;
    }
    return 150;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0)
    {
        return 1;
    }
    else if (section == 1)
    {
        return 3;
    }
    else
    {
        return 2;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MainPageTableViewCell *cell;
 
    if (indexPath.section == 0)
    {
        static NSString *cellIdentifier = @"cell3";
        cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (!cell)
        {
            cell = [[NSBundle mainBundle] loadNibNamed:@"MainPageTableViewCell" owner:self options:nil][2];
        }
        cell.readPrice.text = @"¥89";
        
        //label加中间线条
        NSString *market = [NSString stringWithFormat:@"¥%@",@"500"];
        NSMutableAttributedString *attributeMarket = [[NSMutableAttributedString alloc] initWithString:market];
        [attributeMarket setAttributes:@{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]} range:NSMakeRange(0,market.length)];
        
        cell.onePrice.attributedText = attributeMarket;
        cell.twoPrice.attributedText = attributeMarket;
        cell.threePrice.attributedText = attributeMarket;

    }
    else if (indexPath.section ==1)
    {
        if (indexPath.row != 2)
        {
            static NSString *cellIdentifier = @"cell2";
            cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
            if (!cell)
            {
                cell = [[NSBundle mainBundle] loadNibNamed:@"MainPageTableViewCell" owner:self options:nil][1];
            }
            cell.cellLabelBgImage.image = [UIImage imageNamed:@"green_bg"];
            cell.cellLabelBgImage2.image = [UIImage imageNamed:@"green_bg"];
            cell.cellprice.text = @"¥789";
            cell.cellPrice2.text = @"¥456";
        }
        else
        {
            static NSString *cellIdentifier = @"cell1";
            cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
            if (!cell)
            {
                cell = [[NSBundle mainBundle] loadNibNamed:@"MainPageTableViewCell" owner:self options:nil][0];
            }
            cell.labelBgImge.image = [UIImage imageNamed:@"green_bg"];
            cell.labelBgimage2.image = [UIImage imageNamed:@"green_bg"];
            cell.labelBgImage3.image = [UIImage imageNamed:@"green_bg"];

        }
    }
    else
    {
        static NSString *cellIdentifier = @"cell1";
        cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (!cell)
        {
            cell = [[NSBundle mainBundle] loadNibNamed:@"MainPageTableViewCell" owner:self options:nil][0];
        }
        cell.labelBgImge.image = [UIImage imageNamed:@"purple_bg"];
        cell.labelBgimage2.image = [UIImage imageNamed:@"purple_bg"];
        cell.labelBgImage3.image = [UIImage imageNamed:@"purple_bg"];
    }
    //cell 没有选中状态
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.delegate = self;

    return cell;
}



#pragma mark -cellDeletgate

- (void)checkProductButton:(UIButton *)button
{
    ProductViewController *productAndDetailVC = [[ProductViewController alloc] init];
    [self.navigationController pushViewController:productAndDetailVC animated:YES];
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    //设置section头部
    UIImageView *sectionImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.size.width, 40)];
    sectionImageView.image = [FRUtils resizeImageWithImageName:[sectionImageArr objectAtIndex:section]];
    
    return sectionImageView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section == 0) {
        return CGFLOAT_MIN;//最小数，相当于0
    }
    else if(section == 1){
        return CGFLOAT_MIN;//最小数，相当于0
    }
    return CGFLOAT_MIN;//机器不可识别，然后自动返回默认高度
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
